<?php

class SearchController extends Henry_Controller_Action
{
    public function init()
    {
        parent::init();
        $this->_helper->layout->setLayout('other');
    }
    public function indexAction(){
       $text = $this->_getParam("search");
       $showtext = $text;
       $text = preg_replace("/\s+/i", " ", $text);
       $text = preg_replace("/^\s/i", "", $text);
       $text = preg_replace("/\s$/i", "", $text);
       $text = preg_replace("/\s/i", "%", $text);
       $text =strtolower($text);
      
       $result = array();
       $news = new Henry_News_News();
       $data = $news->fetchAll("LCASE(clip_title) like '%$text%'");
       foreach ($data as $item){
           $tmp = array(
               'title' => $item['clip_title'],
               'link'  => "/tintuc/detail/id/".$item['clip_id']
           );
           $result[] = $tmp;
       }

       $service = new Henry_Services_Services();
       $data = $service->fetchAll("LCASE(clip_title) like '%$text%'");
        foreach ($data as $item){
           $tmp = array(
               'title' => $item['clip_title'],
               'link'  => "/services/index/id/".$item['categories_id']."/sid/".$item['clip_id']
           );
            $result[] = $tmp;
       }

       $gioithieu = new Henry_Gioithieu_Gioithieu();
       $data = $gioithieu->fetchAll("LCASE(clip_title) like '%$text%'");
        foreach ($data as $item){
           $tmp = array(
               'title' => $item['clip_title'],
               'link'  => "/gioithieu/index/id/".$item['clip_id']
           );
            $result[] = $tmp;
       }

       $doitac = new Henry_Doitac_Doitac();
       $data = $doitac->fetchAll("LCASE(clip_title) like '%$text%'");
        foreach ($data as $item){
           $tmp = array(
               'title' => $item['clip_title'],
               'link'  => "/doitac/detail/id/".$item['clip_id']
           );
           $result[] = $tmp;
       }

       $event = new Henry_Event_Event();
        $data = $event->fetchAll("LCASE(clip_title) like '%$text%'");
        foreach ($data as $item){
           $tmp = array(
               'title' => $item['clip_title'],
               'link'  => "/event/index/id/".$item['clip_id']
           );
           $result[] = $tmp;
       }

       $this->view->result = $result;
       $this->view->textsearch = $showtext;
       
    }
}

